package org.example.managere.util;

import org.springframework.data.jpa.domain.Specification;
import jakarta.persistence.criteria.Predicate;
import org.springframework.util.StringUtils;
import org.springframework.data.domain.Sort;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 高级搜索工具类
 */
public class AdvancedSearchUtil {

    /**
     * 构建高级搜索条件
     * @param searchParams 搜索参数Map
     * @return Specification对象
     */
    public static <T> Specification<T> buildAdvancedSearchSpecification(Map<String, String> searchParams) {
        return (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();

            // 遍历搜索参数
            for (Map.Entry<String, String> entry : searchParams.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();

                // 如果值为空，跳过
                if (!StringUtils.hasText(value)) {
                    continue;
                }

                // 处理日期范围
                if (key.endsWith("Start") || key.endsWith("End")) {
                    String fieldName = key.substring(0, key.length() - 5); // 去掉Start或End
                    LocalDateTime dateValue = LocalDateTime.parse(value);
                    
                    if (key.endsWith("Start")) {
                        predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(fieldName), dateValue));
                    } else {
                        predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(fieldName), dateValue));
                    }
                }
                // 处理精确匹配
                else if (key.startsWith("exact_")) {
                    String fieldName = key.substring(6); // 去掉exact_
                    predicates.add(criteriaBuilder.equal(root.get(fieldName), value));
                }
                // 处理模糊匹配
                else {
                    predicates.add(criteriaBuilder.like(root.get(key), "%" + value + "%"));
                }
            }

            // 组合所有条件
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };
    }

    /**
     * 构建排序条件
     * @param sortParams 排序参数，格式：field1,asc;field2,desc
     * @return 排序条件列表
     */
    public static List<Sort.Order> buildSortOrders(String sortParams) {
        List<Sort.Order> orders = new ArrayList<>();
        
        if (StringUtils.hasText(sortParams)) {
            String[] sortArray = sortParams.split(";");
            for (String sort : sortArray) {
                String[] parts = sort.split(",");
                if (parts.length == 2) {
                    String field = parts[0];
                    String direction = parts[1];
                    orders.add(new Sort.Order(
                        direction.equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC,
                        field
                    ));
                }
            }
        }
        
        return orders;
    }
} 